﻿Imports System.Data.SqlClient

Partial Class Tickets_Nouveau
    Inherits System.Web.UI.Page

    ''' <summary>
    ''' Default values
    ''' </summary>
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Me.FormViewTicket.CurrentMode = FormViewMode.Insert Then
            CType(Me.FormViewTicket.FindControl("DropDownListPriorite"), DropDownList).SelectedValue = "3"
        End If
    End Sub

    ''' <summary>
    ''' Ticket author
    ''' </summary>
    Protected Sub FormViewTicket_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles FormViewTicket.ItemInserting
        Me.SqlTicket.InsertParameters("ticket_auteur").DefaultValue = HttpContext.Current.User.Identity.Name
    End Sub

    ''' <summary>
    ''' Comment insertion
    ''' </summary>
    Protected Sub FormViewTicket_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertedEventArgs) Handles FormViewTicket.ItemInserted
        Dim commentaire As String = CType(Me.FormViewTicket.FindControl("FCKeditorCommentaire"), FredCK.FCKeditorV2.FCKeditor).Value

        Dim conn As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("HelpDeskCS").ToString)
        conn.Open()
        Dim cmd As New SqlCommand("SELECT MAX(ticket_id) AS ticket_id FROM Ticket", conn)
        Dim ticket_id As Integer = cmd.ExecuteScalar

        cmd = New SqlCommand("INSERT INTO Commentaire (ticket_id, commentaire_auteur, commentaire_contenu)" & _
        " VALUES (@ticket_id, @commentaire_auteur, @commentaire_contenu)", conn)
        With cmd.Parameters
            .Add(New SqlParameter("ticket_id", ticket_id))
            .Add(New SqlParameter("commentaire_auteur", HttpContext.Current.User.Identity.Name))
            .Add(New SqlParameter("commentaire_contenu", commentaire))
        End With
        cmd.ExecuteNonQuery()

        conn.Close()

        Response.Redirect("Liste.aspx")
    End Sub

End Class
